﻿using System;
using System.Web.Mvc;
using System.Web.WebPages;
using Forest.Cat.Domain;
using Forest.Core.Data;
using Forest.Core.Infrastructure;
using Forest.Web.Framework.Themes;
using System.IO;

namespace Forest.Web.Framework.ViewEngines.Razor {
    public abstract class WebViewPage<TModel> : System.Web.Mvc.WebViewPage<TModel> {

      
        private IWorkContext _workContext;

        

        public IWorkContext WorkContext {
            get {
                return _workContext;
            }
        }

        public override void InitHelpers() {
            base.InitHelpers();

            
        }

        public HelperResult RenderWrappedSection(string name, object wrapperHtmlAttributes) {
            Action<TextWriter> action = delegate(TextWriter tw) {
                                    var htmlAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(wrapperHtmlAttributes);
                                    var tagBuilder = new TagBuilder("div");
                                    tagBuilder.MergeAttributes(htmlAttributes);

                                    var section = RenderSection(name, false);
                                    if (section != null) {
                                        tw.Write(tagBuilder.ToString(TagRenderMode.StartTag));
                                        section.WriteTo(tw);
                                        tw.Write(tagBuilder.ToString(TagRenderMode.EndTag));
                                    }
                                };
            return new HelperResult(action);
        }

        public HelperResult RenderSection(string sectionName, Func<object, HelperResult> defaultContent) {
            return IsSectionDefined(sectionName) ? RenderSection(sectionName) : defaultContent(new object());
        }

        public override string Layout {
            get {
                var layout = base.Layout;

                if (!string.IsNullOrEmpty(layout)) {
                    var filename = System.IO.Path.GetFileNameWithoutExtension(layout);
                    ViewEngineResult viewResult = System.Web.Mvc.ViewEngines.Engines.FindView(ViewContext.Controller.ControllerContext, filename, "");

                    if (viewResult.View != null) {
                        layout = (viewResult.View as RazorView).ViewPath;
                    }
                }

                return layout;
            }
            set {
                base.Layout = value;
            }
        }

        /// <summary>
        /// Return a value indicating whether the working language and theme support RTL (right-to-left)
        /// </summary>
        /// <returns></returns>
        public bool ShouldUseRtlTheme() {
            var supportRtl = false;
            if (supportRtl) {
                //ensure that the active theme also supports it
                var themeProvider = EngineContext.Current.Resolve<IThemeProvider>();
                var themeContext = EngineContext.Current.Resolve<IThemeContext>();
                supportRtl = themeProvider.GetThemeConfiguration(themeContext.WorkingDesktopTheme).SupportRtl;
            }
            return supportRtl;
        }
    }

    public abstract class WebViewPage : WebViewPage<dynamic> {
    }
}
